import { Args, Mutation, Parent, Query, ResolveProperty, Resolver } from "@nestjs/graphql";
import { Article } from "src/entities/article.entity";
import { Collection } from "src/entities/collection.entity";
import { User } from "src/entities/user.entity";
import { CollectionInput, CollectionSortInput, CollectionWhereInput, LabelInput, PageInput } from "src/graphql";
import { ArticleService } from "src/services/article/article.service";
import { CollectionService } from "src/services/collection/collection.service";
import { UserService } from "src/services/user/user.service";

@Resolver('Collection')
export class CollectionResolver {
  constructor(
    private readonly collectionService: CollectionService,
    private readonly userService: UserService,
    private readonly articleService: ArticleService,
  ) {}

  @Query('collection')
  async getCollectionById(@Args() id: number) {
    return await this.collectionService.findOne(id);
  }

  @Query('collections')
  async getCollections(@Args('page') page: PageInput,@Args('sort') sort:CollectionSortInput,@Args('where') where:CollectionWhereInput) {
    return await this.collectionService.getPagination(page,sort,where);
  }

  @Mutation()
  async addCollection(@Args('collection') collection:CollectionInput) {
    return await this.collectionService.addOne(<Collection>collection);
  }

  @Mutation()
  async removeCollection(@Args() id: number) {
    return await this.collectionService.removeOne(id);
  }

  @Mutation()
  async modifyCollection(@Args('id') id: number, @Args('collection') collection:CollectionInput) {
    return await this.collectionService.modifyOne(id,<Collection>collection);
  }

  @ResolveProperty(() => User,{
    name:'_user'
  })
  async user(@Parent() collection){
    const { user_id } = collection;
    return await this.userService.findOne(user_id);
  }

  @ResolveProperty(() => Article,{
    name:'_article'
  })
  async article(@Parent() collection){
    const { article_id } = collection;
    return await this.articleService.findOne(article_id);
  }
}